import {
  getLostOrder,
  getSellOrder,
  getWantOrder,
  getSellMoneyOrder,
  getOrderRate,
  getLostRate
} from '../../api/explosion'
import {
  getMonthSaleRank,
  getWantOrderRank,
  getComeOrderRank,
  getSellOrderRank,
  getUnsellOrderRank
} from '../../api/sale'
import {
  LOST_ORDER_LIST,
  SELL_ORDER_LIST,
  WANT_ORDER_LIST,
  SELL_MONEY_ORDER_LIST,
  ORDER_RATE_LIST,
  LOST_RATE_LIST,
  MONTH_SALE_RANK_LIST,
  WANT_ORDER_RANK_LIST,
  COME_ORDER_RANK_LIST,
  SELL_ORDER_RANK_LIST,
  UNSELL_ORDER_RANK_LIST
} from '../motations-type'

export default {
  state: {
    lostOrderList: [],
    sellOrderList: [],
    wantOrderList: [],
    sellMoneyOrderList: [],
    orderRateList: [],
    lostRateList: [],
    monthSaleRankList: [],
    wantOrderRankList: [],
    comeOrderRankList: [],
    sellOrderRankList: [],
    unsellOrderRankList: []
  },
  getters: {
    lostOrderList: state => state.lostOrderList,
    sellOrderList: state => state.sellOrderList,
    wantOrderList: state => state.wantOrderList,
    sellMoneyOrderList: state => state.sellMoneyOrderList,
    orderRateList: state => state.orderRateList,
    lostRateList: state => state.lostRateList,
    monthSaleRankList: state => state.monthSaleRankList,
    wantOrderRankList: state => state.wantOrderRankList,
    comeOrderRankList: state => state.comeOrderRankList,
    sellOrderRankList: state => state.sellOrderRankList,
    unsellOrderRankList: state => state.unsellOrderRankList
  },
  mutations: {
    [LOST_ORDER_LIST] (state, data) {
      state.lostOrderList = data
    },
    [SELL_ORDER_LIST] (state, data) {
      state.sellOrderList = data
    },
    [WANT_ORDER_LIST] (state, data) {
      state.wantOrderList = data
    },
    [SELL_MONEY_ORDER_LIST] (state, data) {
      state.sellMoneyOrderList = data
    },
    [ORDER_RATE_LIST] (state, data) {
      state.orderRateList = data
    },
    [LOST_RATE_LIST] (state, data) {
      state.lostRateList = data
    },
    [MONTH_SALE_RANK_LIST] (state, data) {
      state.monthSaleRankList = data
    },
    [WANT_ORDER_RANK_LIST] (state, data) {
      state.wantOrderRankList = data
    },
    [COME_ORDER_RANK_LIST] (state, data) {
      state.comeOrderRankList = data
    },
    [SELL_ORDER_RANK_LIST] (state, data) {
      state.sellOrderRankList = data
    },
    [UNSELL_ORDER_RANK_LIST] (state, data) {
      state.unsellOrderRankList = data
    }
  },
  actions: {
    async getLostOrderList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getLostOrder(data)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          name: item.pname,
          value: item.nums
        }]
      })
      commit(LOST_ORDER_LIST, newData)
      return resp
    },
    async getSellOrderList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getSellOrder(data)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          name: item.pname,
          value: item.nums
        }]
      })
      commit(SELL_ORDER_LIST, newData)
      return resp
    },
    async getWantOrderList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getWantOrder(data)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          name: item.pname,
          value: item.nums
        }]
      })
      commit(WANT_ORDER_LIST, newData)
      return resp
    },
    async getSellMoneyOrderList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getSellMoneyOrder(data)
      resp.data.data.rows.map((item) => {
        console.log(item.nums)
        newData = [...newData, {
          name: item.pname,
          value: item.nums && item.nums !== '0' ? item.nums : 0
        }]
      })
      commit(SELL_MONEY_ORDER_LIST, newData)
      return resp
    },
    async getOrderRateList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let orderList = {}
      let countList = []
      let dateList = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getOrderRate(data)
      resp.data.data.rows.map((item) => {
        countList.push(item.nums)
        dateList.push(item.pname)
      })
      orderList.date = dateList
      orderList.count = countList
      commit(ORDER_RATE_LIST, orderList)
      return resp
    },
    async getLostRateList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let lostList = {}
      let countList = []
      let dateList = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getLostRate(data)
      resp.data.data.rows.map((item) => {
        countList.push(item.nums)
        dateList.push(item.pname)
      })
      lostList.date = dateList
      lostList.count = countList
      commit(LOST_RATE_LIST, lostList)
      return resp
    },
    async getMonthSaleRankList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let monthSaleList = {}
      let countList = []
      let dateList = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getMonthSaleRank(data)
      resp.data.data.rows.map((item) => {
        let nums = item.nums ? item.nums : 0
        countList.push(nums)
        dateList.push(item.pname)
      })
      monthSaleList.date = dateList
      monthSaleList.count = countList
      commit(MONTH_SALE_RANK_LIST, monthSaleList)
      return resp
    },
    async getWantOrderRankList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let wantOrderList = {}
      let countList = []
      let dateList = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getWantOrderRank(data)
      resp.data.data.rows.map((item) => {
        let nums = item.nums ? item.nums : 0
        countList.push(nums)
        dateList.push(item.pname)
      })
      wantOrderList.date = dateList
      wantOrderList.count = countList
      commit(WANT_ORDER_RANK_LIST, wantOrderList)
      return resp
    },
    async getComeOrderRankList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let comeOrderList = {}
      let countList = []
      let dateList = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getComeOrderRank(data)
      resp.data.data.rows.map((item) => {
        let nums = item.nums ? item.nums : 0
        countList.push(nums)
        dateList.push(item.pname)
      })
      comeOrderList.date = dateList
      comeOrderList.count = countList
      commit(COME_ORDER_RANK_LIST, comeOrderList)
      return resp
    },
    async getSellOrderRankList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let sellOrderList = {}
      let countList = []
      let dateList = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getSellOrderRank(data)
      resp.data.data.rows.map((item) => {
        let nums = item.nums ? item.nums : 0
        countList.push(nums)
        dateList.push(item.pname)
      })
      sellOrderList.date = dateList
      sellOrderList.count = countList
      commit(SELL_ORDER_RANK_LIST, sellOrderList)
      return resp
    },
    async getUnsellOrderRankList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let unsellOrderList = {}
      let countList = []
      let dateList = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getUnsellOrderRank(data)
      resp.data.data.rows.map((item) => {
        let nums = item.nums ? item.nums : 0
        countList.push(nums)
        dateList.push(item.pname)
      })
      unsellOrderList.date = dateList
      unsellOrderList.count = countList
      commit(UNSELL_ORDER_RANK_LIST, unsellOrderList)
      return resp
    }
  }
}
